<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version

  $Source$
  $Revision$
  $Author$
  $Date$
**********************************************/
if (!defined('INSTALL')) { exit; }

$upgrade_failed = "<br /><br />Please contact the CPG-Nuke Dev Team about the error<br />You cannot continue with the installation";

if (version_compare($version, '9.3.2.2', '<')) {
	exit('Upgrade needs atleast v9.3.2.2');
}

if (version_compare($version, '10.0.9.23', '<')) {
	$data = $db->list_columns($prefix.'_comments');
	if (isset($data['msg_author'])) {
		$installer->add_query('DEL', 'comments', 'msg_author');
		$installer->add_query('DEL', 'comments', 'msg_body');
		$installer->add_query('DEL', 'comments', 'msg_date');
		$installer->add_query('DEL', 'comments', 'author_md5_id');
		$installer->add_query('DEL', 'comments', 'author_id');
		$installer->add_query('DEL', 'comments', 'msg_raw_ip');
		$installer->add_query('DEL', 'comments', 'msg_hdr_ip');
		$installer->add_query('DROP_INDEX', 'comments', 'com_pic_id');
	}

	$data = $db->list_indexes($prefix.'_config_custom');
	if (isset($data['unique_cfg'])) $installer->add_query('DROP_INDEX', 'config_custom', 'unique_cfg');

	$data = $db->list_columns($prefix.'_counter');
	if (isset($data['agent'])) $installer->add_query('DROP_INDEX', 'counter', 'agent');

	$data = null;
}
if (version_compare($version, '10.0.9.28', '<')) {
	$installer->add_query('UPDATE', 'counter', 'type="bot", var="Other" WHERE type="browser" AND var="Bot"', 'type="browser" var="Bot" WHERE type="bot" AND var="Other"');
}
if (version_compare($version, '10.0.9.29', '<')) {
	$ips = $domains = array();
	$result = $db->query('SELECT ban_ipv4_s, ban_ipv4_e, ban_ipn, ban_string, ban_type, ban_details FROM {security} WHERE ban_type IN (0,2,3,8)', Poodle_SQL::ADD_PREFIX);
	while ($row = $result->fetch_assoc()) {
		switch ($row['ban_type']):
			case 0:
			case 8:
				$ipn_s = $db->escapeBinary($row['ban_ipn'] ?: inet_pton(long2ip($row['ban_ipv4_s'])));
				$ipn_e = $row['ban_ipv4_e'] ? $db->escapeBinary(inet_pton(long2ip($row['ban_ipv4_e']))) : 'DEFAULT';
				$details = $row['ban_details'] ? "'".$db->escape_string($row['ban_details'])."'" : 'DEFAULT';
				$ips[] = "($ipn_s, $ipn_e, {$row['ban_type']}, $details)";
				break;
			case 2:
			case 3:
				$domains[] = "('".$db->escape_string($row['ban_string'])."', {$row['ban_type']})";
				break;
			default:
			break;
		endswitch;
	}
	if (count($insert)) {
		$installer->add_query('INSERT_MULTIPLE', 'security_ips', array('ipn_s, ipn_e, type, details', implode(',', $insert))/*, 'type IN (0,8)'*/);
	}
	if (count($domains)) {
		$installer->add_query('INSERT_MULTIPLE', 'security_domains', array('ban_string, ban_type', implode(',', $domains))/*, 'type IN (2,3)'*/);
	}
	$db->query("DELETE FROM {$prefix}_security_agents WHERE agent_name = 'MSN'");
	//$installer->add_query('DROP', 'security');
}
if (version_compare($version, '10.0.9.35', '<')) {
	list($hits) = $db->uFetchRow("SELECT SUM(hits) FROM {$prefix}_stats_hour");
	list($count) = $db->uFetchRow("SELECT SUM(count) FROM {$prefix}_counter WHERE type IN ('browser','bot')");
	if ($hits > $count) $db->query("UPDATE {$prefix}_counter SET count=count+$hits-$count WHERE type='browser' AND var='Other'");
}